"use strict";
const mkdirp = require('mkdirp');
const url = require('url');
const http = require('http');
const iconv = require('iconv-lite');
const os = require('os');
const fs = require('fs');

var targetDir = os.homedir() + '/Documents/Book/盗墓笔记/藏海花2';
//var indexPage = "http://www.daomubiji.com/";
//var indexPage = "http://www.idaomu.com/dao-mu-bi-ji-he-sui-pian";
var indexPage = "http://www.daomubiji.org/zang-hai-hua-2";

//var volumnRule = '<dt>[^<>]*(第[^<>]+卷[^<>]*)</dt>';
//var volumnRule = '<td colspan="3"><center><h2>([^<]+)</h2></center></td>';
var volumnRule = '(_@NO_VOLUMN@_)'; //如果没有分卷，就随便弄个不可能出现的匹配规则...
//var subjectRule = '<dd><a href="(http://www.laidudu.com/modules/article/reader.php\\?[^<>"]+)">([^<]+)</a></dd>';
//var subjectRule = '<td><a href="(http://www.idaomu.com/[^<>]+)">([^<]+)</a></td>';
//var subjectRule = '<li><a href="(http://www.idaomu.com/[^<>]+)">([^<]+)</a></li>';
var subjectRule = '<td><a href="(http://www.daomubiji.org/[^<>]+)" rel="bookmark">([^<]+)</a></td>';
var srcEncoding = 'utf-8';

var idleTime = 5000;

var useProxy = false;
var proxyServer = ""; 	// proxy server address
var proxyPort = 8080;	// proxy server port
var proxyAuth = "";

function download(address, callback) {
	//console.info("download: " + address);
	let urlObj = url.parse(address);
	let options;
	if (useProxy) {
		options = {
			hostname: proxyServer,
			port: proxyPort,
			path: address,
			headers: {
				"Proxy-Authentication": "Base " + new Buffer(proxyAuth).toString("base64"),
				"Host": urlObj.host
			}
		};
	} else {
		options = {
			hostname: urlObj.hostname,
			port: urlObj.port,
			path: address		
		};
	}

	let req = http.request(options, function(res){
		//console.log(url + ' STATUS: ' +  res.statusCode);
		//console.log('HEADERS: ' + JSON.stringify(res.headers));
		let buffers = [], size = 0;
		res.on('data', function (chunk) {
			buffers.push(chunk);
			size += chunk.length;
		});
		res.on('end', function() {
			let buffer = new Buffer(size), pos = 0;
			for(let i = 0, l = buffers.length; i < l; i++) {
				buffers[i].copy(buffer, pos);
				pos += buffers[i].length;
			}
			if (size < 20) {
				console.log(address + ' STATUS: ' +  res.statusCode);
				callback && callback(new Error('Connection lost!'), null);
			} else
				callback && callback(null, iconv.decode(buffer, srcEncoding));
		});
		res.on('close', function() {
			callback && callback(new Error('Connection lost!'), null);
		});
	});
	req.on('error', function(e) {
		callback && callback(e, null);
	});
	req.end();
}

mkdirp(targetDir, function (err) {
    if (err) 
		console.error(err)
	else
		run();
});

var currentDir = targetDir;

function run() {
	var downloadCount = 0;
	var volumnCount = 0;
	download(indexPage, function(err, content){
		if (err) {
			console.error(err.message);
			fs.close(fd);
			return;
		}
		console.info("index downloaded!");
		let patt = volumnRule + "|" + subjectRule;
		let pattern = new RegExp(patt, 'igm');
		//console.log(content);
		let result;
		let sectionId = 0;
		let downloadIterator = (function* () {
			while ((result = pattern.exec(content)) != null)  {
				if (result[1] != null) {
					volumnCount++;
					let volumn = result[1].trim();
					console.log(volumn);
					currentDir = targetDir + "/" + volumnCount + "." + volumn.replace("/", "");
					mkdirp(currentDir, function (err) {
						if (err) 
							throw new Error("Create volumn directory failed: " + err);
						else {
							downloadIterator.next();
						}
					});
				} else {
					downloadCount++;
					var chapter = result[3].replace("/", "");
					var targetFile = currentDir + "/" + downloadCount + "." + chapter + ".txt";
					if (fs.existsSync(targetFile))
						continue;
					var fd = fs.openSync(targetFile, 'w+');
					download(result[2], function(err, content){
						if (err) {
							console.error(downloadCount + ' ' + result[3] + ' ... Failed');
							fs.close(fd);
							return;
						}
						fs.writeSync(fd, '\n\n' + chapter + '\n\n');
						fs.writeSync(fd, content);
						fs.close(fd);
						console.log(downloadCount + ' ' + chapter + ' ... OK');
						setTimeout(function(){
							downloadIterator.next();
						}, idleTime);
					});
				}
				yield;
			}
		})();
		downloadIterator.next();
	});
}



